package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.bean.Composition;
import com.bean.SqlObject;
import com.bean.User;
import com.bean.UserInfo;
import com.dao.*;
import com.util.MD5;


@Controller
public class HealController {
	private DataDao userDao = null;
	private DataDao userInfoDao = null;
	private DataDao compositionDao = null;
	
	public HealController(){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new 
				ClassPathXmlApplicationContext("bean-spring.xml");
		userDao =(DataDao) ctx.getBean("user");
		userInfoDao =(DataDao) ctx.getBean("userInfo");
		compositionDao =(DataDao) ctx.getBean("composition");
	}
	
	@RequestMapping(value="/login")//登录验证,
	public void login(@RequestParam String name, String password, HttpServletRequest req, HttpServletResponse resp)
			throws IOException, SQLException {
		System.out.println(1111111111);
		User user = (User) userDao.find(name);
		UserInfo userInfo = (UserInfo) userInfoDao.find(name);
		HttpSession session = req.getSession();
		PrintWriter out=resp.getWriter();
		if (password != null && user != null && MD5.stringMD5(password).equals(user.getPassword())) {//
			//session.setAttribute("message", "");
			session.setAttribute("pass", userInfo);
			out.write("true");
			System.out.println(2222222);  
		} else {
			session.setAttribute("message", "用户名或密码错误!");
			out.write("false");
			System.out.println(33333333);
		}
	}
	
	@RequestMapping(value="/logincheck")//登录状态验证
	public void logincheck(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, SQLException {
		
		HttpSession session = req.getSession();
		PrintWriter out=resp.getWriter();
		if (session.getAttribute("pass")!=null) {//
			out.write("true");
		} else {
			out.write("false");
		}
	}

	@RequestMapping("/exit")
	public void exit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		session.invalidate();
		resp.sendRedirect("http://localhost:8080/health/login.htm");

	}

	@RequestMapping("/jsp/recipeAjax")
	public void checkRecipe(@RequestParam String submit, HttpServletRequest req, HttpServletResponse resp) {
		HttpSession session = req.getSession();

		if ("reset".equals(submit)) {
			session.removeAttribute("nutrient");
			session.removeAttribute("nutrient2");
			session.removeAttribute("nutrient1");
			try {
				resp.sendRedirect("recipe.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@RequestMapping("/jsp/recipe") // 食谱分析
	public void functionMenu(@RequestParam String staple_food, String mainCount, String meat, String meatCount,
			String melon, String melonCount, String leaf, String leafCount, String other, String otherCount,
			HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Composition nutrient = new Composition();
		Composition food1 = null, food2 = null, food3 = null, food4 = null, food5 = null;
		if (mainCount != "0" && mainCount != "") {// 如果没填质量数则相应的食物对象为空值;
			food1 = (Composition) compositionDao.find(staple_food);
			food1.multiply(Double.parseDouble(mainCount));
		}
		if (!"0".equals(meatCount) && !"".equals(meatCount)) {
			food2 = (Composition) compositionDao.find(meat);
			System.out.println(33333333);
			food2.multiply(Double.parseDouble(meatCount));
		}
		if (!"0".equals(melonCount) && !"".equals(melonCount)) {
			food3 = (Composition) compositionDao.find(melon);
			food3.multiply(Double.parseDouble(melonCount));
		}
		if (!"0".equals(leafCount) && !"".equals(leafCount)) {
			food4 = (Composition) compositionDao.find(leaf);
			food4.multiply(Double.parseDouble(leafCount));
		}
		if (!"0".equals(otherCount) && !"".equals(otherCount)) {
			food5 = (Composition) compositionDao.find(other);
			food5.multiply(Double.parseDouble(otherCount));
		}
		nutrient.add(food1).add(food2).add(food3).add(food4).add(food5).round();// 所有成分营养相加后格式化
		HttpSession session = req.getSession();
		if (session.getAttribute("nutrient") == null) {
			session.setAttribute("nutrient", nutrient);
		} else if (session.getAttribute("nutrient2") == null) {
			session.setAttribute("nutrient2", nutrient);
		} else if (session.getAttribute("nutrient1") == null) {
			session.setAttribute("nutrient1", nutrient);
		}
		resp.sendRedirect("http://localhost:8080/health/jsp/recipe.jsp");
	}

}
